package com.xca.is.views;

import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.transition.Fade;
import android.util.Log;
import android.view.View;
import android.view.Window;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.xca.is.R;
import com.xca.is.componse.BottomView;
import com.xca.is.dataIntent.FragmentIsDestory;
import com.xca.is.dataIntent.FragmentIsDestroy2;
import com.xca.is.fragment.AiFragment;
import com.xca.is.fragment.IndexFragment;
import com.xca.is.fragment.MenuFragment;
import com.xca.is.fragment.VideoFragment;
import com.xca.is.utils.ReplaceFragmentUtils;

public class MainActivity extends AppCompatActivity {
    private BottomView bottomView ;
    private ReplaceFragmentUtils replaceFragmentUtils ;


    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        returnActivity();
        setContentView(R.layout.activity_main);
        bottomView = findViewById(R.id.bottomView);
        replaceFragmentUtils = new ReplaceFragmentUtils(getSupportFragmentManager());
        replaceFragmentUtils.replaceFragment(new IndexFragment(),R.id.contentView);
        // bottomBtnClick();
        for (int i = 0; i < 4; i++){
            bottomBtnClick(i);
        }
        setFragmentVideo();
    }

    private void bottomBtnClick(int index){
        Fragment[] fragments = {new IndexFragment(),new VideoFragment(),new AiFragment() ,new MenuFragment() };
        bottomView.setBtnClickListener(index, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                replaceFragmentUtils.replaceFragment(fragments[index],R.id.contentView);
                bottomView.setBtnSelected(index);
                SharedPreferences sharedPreferences = getSharedPreferences("is" , MODE_PRIVATE) ; // 获取数据
                SharedPreferences.Editor editor = sharedPreferences.edit() ;
                boolean isVideoPlay = sharedPreferences.getBoolean("isPlay" , false) ;
                if (isVideoPlay && index!=1){
                    editor.putBoolean("isPlay" , false) ;
                } else if (!isVideoPlay && index==1) {
                    editor.putBoolean("isPlay" , true) ;
                }else if (!isVideoPlay && index != 1){
                    Log.d("is test", "onClick: ");
                }
                editor.apply();
            }
        });
    }
    private void returnActivity(){
        getWindow().requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS) ;

        Fade fade = new Fade() ;
        fade.setDuration(1000) ;
        getWindow().setEnterTransition(fade);
        getWindow().setExitTransition(fade);
    }

    private void setFragmentVideo(){
        SharedPreferences sharedPreferences = getSharedPreferences("is" , MODE_PRIVATE) ; // 获取数据
        SharedPreferences.Editor editor = sharedPreferences.edit() ;
        editor.putBoolean("isPlay" , false) ;
        editor.apply();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setFragmentVideo();
    }
}